<?php
// +----------------------------------------------------------------------
// | Jqcms [ Avenue to Jane ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://jqcms.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: hyacinth <845077398@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\BaseController;

class NavController extends BaseController {
    
    public function index(){
       
        $nav = M('Nav');
        $where = "";
        $count = $nav->where($where)->count();
        $p = getpage($count,10);
        $list = $nav->field(true)->where($where)->order('id desc')->limit($p->firstRow, $p->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->display();
        
    }
    public function create(){
        
        $nav= D('Nav');
        if(IS_POST){
            if($nav->create()){
                $nav->add();
                $this->success('添加成功!','index');
            }else{
               $this->error($nav->getError());
            }  
        }else{
            $data['cate'] = $nav->getNavcate();
            $data['artcate'] = $nav->getArtcate();
            $data['page'] = $nav->getPage();
            $this->assign('data',$data);
            $this->display();
        }
           
    }
    //编辑
    public function edit($id){
        
        $nav = D('Nav');
        if (IS_POST) {
            
            if($nav->create()){
                $nav->save();
                $this->success('编辑成功!','index');
            }else{
                $this->error($nav->getError());
            } 
        }else {
            $data = $nav->find($id);
            $data['cate'] = $nav->getNavcate();
            $data['artcate'] = $nav->getArtcate();
            $data['page'] = $nav->getPage();
            $data['defdata'] = $nav->getBlock($data['block'],$data['cid']);
            $this->assign('data',$data);
            $this->display();
        }
    }
    //单删 
    public function del($id){
    
        $nav = M('Nav');
        $del = $nav->delete($id);
        $cid = $nav->where(array('pid'=>$id))->select();
        if ($cid==null){
        
            $del = $nav->delete($id);
            if($del !==0 && $del !==false){
                $this->success('删除成功!');
            }else {
                $this->error('删除失败!');
            }
        
        }else {
        
            $this->error('该栏目下存在栏目!');
        }
        
        
    }
    //多删
    public function delselect(){
        
        $nav = M('Nav');
        $idarr = $_POST['id'];
        $idstr = implode(',',$idarr);
        $del = $nav->delete($idstr);
        
        if($del !==0 && $del !==false){
            $this->success('删除成功!');
        }else {
            $this->error('删除失败!'); 
        }
        
    }
    
  
    
}